package com.ruoyi.pom.service.impl;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.pom.domain.PomEbbinghausReview;
import com.ruoyi.pom.mapper.PomTasksMapper;
import com.ruoyi.pom.domain.PomTasks;
import com.ruoyi.pom.service.IPomTasksService;

import javax.annotation.Resource;
import javax.annotation.Resources;

/**
 * tasksService业务层处理
 *
 * @author ruoyi
 * @date 2025-01-10
 */
@Service
public class PomTasksServiceImpl implements IPomTasksService {
    @Resource
    private PomTasksMapper pomTasksMapper;

    /**
     * 查询tasks
     *
     * @param id tasks主键
     * @return tasks
     */
    @Override
    public PomTasks selectPomTasksById(Long id) {
        return pomTasksMapper.selectPomTasksById(id);
    }

    /**
     * 查询tasks列表
     *
     * @param pomTasks tasks
     * @return tasks
     */
    @Override
    public List<PomTasks> selectPomTasksList(PomTasks pomTasks) {
        return pomTasksMapper.selectPomTasksList(pomTasks);
    }

    /**
     * 新增tasks
     *
     * @param pomTasks tasks
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPomTasks(PomTasks pomTasks) {
        pomTasks.setCreateTime(DateUtils.getNowDate());
        int rows = pomTasksMapper.insertPomTasks(pomTasks);
        if (pomTasks.getPomEbbinghausReviewList().isEmpty()) {
            PomEbbinghausReview pomEbbinghausReview = new PomEbbinghausReview();
            BeanUtils.copyProperties(pomTasks, pomEbbinghausReview);

            LocalDateTime now = LocalDateTime.now();

            // 加上20分钟
            LocalDateTime plus20Minutes = now.plusMinutes(20);
            Date plus20Minutes_=convertLocalDateTimeToDate(plus20Minutes);
            pomEbbinghausReview.setReviewTime1(plus20Minutes_);
            // 加上1天
            LocalDateTime plus1Day = now.plusDays(1);
            Date plus1Day_=convertLocalDateTimeToDate(plus1Day);
            pomEbbinghausReview.setReviewTime2(plus1Day_);

            // 加上1周
            LocalDateTime plus1Week = now.plusWeeks(1);
            Date plus1Week_=convertLocalDateTimeToDate(plus1Week);
            pomEbbinghausReview.setReviewTime3(plus1Week_);

            // 加上1个月
            LocalDateTime plus1Month = now.plusMonths(1);
            Date plus1Month_=convertLocalDateTimeToDate(plus1Month);
            pomEbbinghausReview.setReviewTime4(plus1Month_);

            // 加上6个月
            LocalDateTime plus6Months = now.plusMonths(6);
            Date plus6Months_=convertLocalDateTimeToDate(plus6Months);
            pomEbbinghausReview.setReviewTime5(plus6Months_);

            pomEbbinghausReview.setInitialStudyTime(pomTasks.getCreateTime());
            pomEbbinghausReview.setTasksId(pomTasks.getId());
            pomEbbinghausReview.setContent(pomTasks.getTitle());

            List<PomEbbinghausReview> pomEbbinghausReviewList=new ArrayList<>();
            pomEbbinghausReviewList.add(pomEbbinghausReview);
            pomTasks.setPomEbbinghausReviewList(pomEbbinghausReviewList);

        }
        insertPomEbbinghausReview(pomTasks);
        return rows;
    }

    /**
     * 修改tasks
     *
     * @param pomTasks tasks
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePomTasks(PomTasks pomTasks) {
        pomTasks.setUpdateTime(DateUtils.getNowDate());
        pomTasksMapper.deletePomEbbinghausReviewByTasksId(pomTasks.getId());
        if (pomTasks.getPomEbbinghausReviewList().isEmpty()) {
            PomEbbinghausReview pomEbbinghausReview = new PomEbbinghausReview();
            BeanUtils.copyProperties(pomTasks, pomEbbinghausReview);

            LocalDateTime now = LocalDateTime.now();

            // 加上20分钟
            LocalDateTime plus20Minutes = now.plusMinutes(20);
            Date plus20Minutes_=convertLocalDateTimeToDate(plus20Minutes);
            pomEbbinghausReview.setReviewTime1(plus20Minutes_);
            // 加上1天
            LocalDateTime plus1Day = now.plusDays(1);
            Date plus1Day_=convertLocalDateTimeToDate(plus1Day);
            pomEbbinghausReview.setReviewTime2(plus1Day_);

            // 加上1周
            LocalDateTime plus1Week = now.plusWeeks(1);
            Date plus1Week_=convertLocalDateTimeToDate(plus1Week);
            pomEbbinghausReview.setReviewTime3(plus1Week_);

            // 加上1个月
            LocalDateTime plus1Month = now.plusMonths(1);
            Date plus1Month_=convertLocalDateTimeToDate(plus1Month);
            pomEbbinghausReview.setReviewTime4(plus1Month_);

            // 加上6个月
            LocalDateTime plus6Months = now.plusMonths(6);
            Date plus6Months_=convertLocalDateTimeToDate(plus6Months);
            pomEbbinghausReview.setReviewTime5(plus6Months_);
            pomEbbinghausReview.setInitialStudyTime(pomTasks.getCreateTime());
            pomEbbinghausReview.setTasksId(pomTasks.getId());
            pomEbbinghausReview.setContent(pomTasks.getTitle());
            List<PomEbbinghausReview> pomEbbinghausReviewList=new ArrayList<>();
            pomEbbinghausReviewList.add(pomEbbinghausReview);
            pomTasks.setPomEbbinghausReviewList(pomEbbinghausReviewList);
        }
        insertPomEbbinghausReview(pomTasks);
        return pomTasksMapper.updatePomTasks(pomTasks);
    }

    /**
     * 批量删除tasks
     *
     * @param ids 需要删除的tasks主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePomTasksByIds(Long[] ids) {
        pomTasksMapper.deletePomEbbinghausReviewByTasksIds(ids);
        return pomTasksMapper.deletePomTasksByIds(ids);
    }

    /**
     * 删除tasks信息
     *
     * @param id tasks主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePomTasksById(Long id) {
        pomTasksMapper.deletePomEbbinghausReviewByTasksId(id);
        return pomTasksMapper.deletePomTasksById(id);
    }

    /**
     * 新增review信息
     *
     * @param pomTasks tasks对象
     */
    public void insertPomEbbinghausReview(PomTasks pomTasks) {
        List<PomEbbinghausReview> pomEbbinghausReviewList = pomTasks.getPomEbbinghausReviewList();
        Long id = pomTasks.getId();
        if (StringUtils.isNotNull(pomEbbinghausReviewList)) {
            List<PomEbbinghausReview> list = new ArrayList<PomEbbinghausReview>();
            for (PomEbbinghausReview pomEbbinghausReview : pomEbbinghausReviewList) {
                pomEbbinghausReview.setTasksId(id);
                list.add(pomEbbinghausReview);
            }
            if (list.size() > 0) {
                pomTasksMapper.batchPomEbbinghausReview(list);
            }
        }
    }


    public static Date convertLocalDateTimeToDate(LocalDateTime localDateTime) {
        // 将 LocalDateTime 转换为 ZonedDateTime
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        // 将 ZonedDateTime 转换为 Date
        return Date.from(zonedDateTime.toInstant());
    }
}
